window.uam = window.uam || {};
uam.Utils = {
		
	/* ************************
	 * Request Codes 
	 * ***********************/
	requestCode : {
		UP_REGISTRATION : 101,
		UP_EDIT : 102,
		UP_VIEW : 103
	},
	
	/* ************************
	 * Response Codes 
	 * ***********************/
	responseCode : {
		OK : 200,
		ERROR : 500,
		// invalid request code
		ERROR_INVALID_REQUEST_CODE : 501,
		ERROR_INVALID_PARAMETERS : 502
	},
	
	/**
	 * ajax call to task
	 * 
	 * @param params:Object request parameter list
	 * @param succcess:Function handle the callback
	 * 
	 */
	callTask:function(params, success){
		
		if(typeof(microstrategy) != 'undefined'){
			var session = String(microstrategy.sessionState);
		}
		
		params = params || {};
		
		params["taskId"] = params["taskId"] || 'UserProfileService';
		params["taskEnv"] = params["taskEnv"] || 'xhr';
        params["taskContentType"] = params["taskContentType"] || 'json';
        params["session"] = params["session"] || String(session);
        params["dateTime"] = params["dateTime"] || (new Date()).getTime();
        
        
        //formValueSubmit:
        success = success || function(data){ console ? console.log("callTask.data = " + data) : alert("callTask.data = " + data); }
		
        console.log(params);
        
		taskUrl = './taskProc';
		$.ajax({
			url:  taskUrl,
			type: 'GET',
			data: params,
			cache: "false",
			dataType: "text",
			async: true, //set to false in case we want it to wait
			success: success ,
			error: function (XMLHttpRequest, textStatus, errorThrown) {
		        alert("XMLHttpRequest="+XMLHttpRequest.responseText+"\ntextStatus="+textStatus+"\nerrorThrown="+errorThrown);
		    } 
		});
	}
};

var formValue = {
	Dashboard:"Dashboard",
	eupDpartmntId:"departmentId",
	eupDpartmntDesc:"departmentDesc",
	eupBusnUCode:"busnUCode",
	eupBsnUCDesc:"busnUCodeDesc",
	eupAmexUCd:"amexUCode",
	eupAmexUDesc:"amexUDesc",
	eupExpEndCd:"excpEndCode",
	radio:"detailAllAccess",
	eupPhone:"1234567890",
	eupEmail:"test@gmail.com",
	eupMFN:"firstname",
	eupMI:"mIn",
	eupLN:"lastname",
	eupMEmail:"manager@gmail.com",
	eupVPName:"VPName",
	eupReason:"reason"
};


/*********************************
 * Edit User Profile
 * ******************************/
uam.eup = uam.eup || {};

uam.eup.loadUserProfile = function (userid){
	
	var params = {
		'taskId' : 'N001',
		'TaskSubID' : 'GetUserProfile',
		'userid' : userid
	};
	uam.Utils.callTask(params, function(responseData) {
		
		eval("var responseContext=" + responseData);
		console.log(responseData);
		
		if(responseContext["status"] == uam.Utils.responseCode.OK){
			
			var data = responseContext["data"];
			
			var userProfile = data.userProfile;
			console.log(userProfile);
			
			for (var i = 0; i < userProfile.length; i++ ) {
				if(!userProfile[i] || userProfile[i].length < 2){
					continue;
				}
				uam.eup.assignValue(userProfile[i][0], userProfile[i][1])
				
			}
			

		}
	});
}

// id : function(value)
uam.eup.assignMapper = {
		"checkbox" : "uam.eup.assignCheckbox",
		"radio3" : "uam.eup.assignRadio3"
};

uam.eup.assignValue = function(id, value) {
	if (uam.eup.assignMapper[id]) {
		// special
		console.log("special: " + uam.eup.assignMapper[id]);
		eval(uam.eup.assignMapper[id]+'(value)');
		
	} else {
		// general
		if ($("#" + id) && value) {
			$("#" + id).val(value);
		}
	}
}
uam.eup.assignRadio3 = function() {
	//XXX unknow logic
	console.log("WARN: Untreated assignment!!!");
}
uam.eup.assignCheckbox = function(value) {
	if (value == 'Y')
		$("#checkbox").attr("checked", true);
}


/**
 * window.onload
 */
$(document).ready(function(){

	// XXX assign userid value
	//get user id from cookie
	var userid = '8165840';
	uam.eup.loadUserProfile(userid);
	$("#eupEmpContID").val(userid);
	
});